import {Component, OnInit, Input, SimpleChange, OnChanges} from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'control-table',
  templateUrl: './control-table.component.html',
  styleUrls: ['control-table.component.scss']
})
export class ControlTableComponent implements OnInit, OnChanges {
  @Input()
  set parmas(value: any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.tabWidth = value["tabWidth"];
    this.todayLeft = value["todayLeft"];
    this.waitLeft = value["waitLeft"];
    this.color = value['color'];
    this.pages = value['pages'];
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  @Input() companyId: any;

  color: string = '#5ef5f9';
  pages: number = 5;
  _parmas: any;
  title: string;
  tabWidth: string;
  todayLeft: string;
  waitLeft: string;
  titleList: any[] = [];
  reportData: any[] = [];

  allData: any[] = [];
  tablesIndex: number = 0;
  itmeData: any[] = [];

  constructor(private apiService: ApiService,
              private msg: ApesMessageService) {
    const time = (1000 * 60) * 10;                          //   下三表  每10分钟请求一次
    setInterval(() => {
      this.allData = [];
      this.getData();
    }, time)

    setInterval(() => {                                     //   每5秒轮播 下三表数据
      this.reportData = [];
      this.pageList(this.allData);
      this.pageList(this.itmeData);
    }, 5000)
  }

  ngOnInit() {
  }

  ngOnChanges(changes: {[propName: string]: SimpleChange}) {
    this.companyId = changes['companyId'].currentValue;
    this.getData();
  }


  getData() {
    if (this.parmas["api"]) {
      this.allData = [];
      this.itmeData = [];
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': {'companyId': this.companyId}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              this.allData = data;
              this.itmeData = data.items;
              this.pageList(this.allData);
              this.pageList(this.itmeData);
            }
          } else {
            this.msg.info(result.msg);

          }
        });

    }
  }

  // 分页显示
  pageList(arrData: any[]) {
    if (arrData !== undefined) {
      const count = arrData.length;
      this.reportData = [];
      let tempIndex = 0;
      for (let i = this.tablesIndex; i < count; i++) {
        if (tempIndex < this.pages) {
          this.reportData.push(arrData[i]);
        } else {
          break;
        }
        tempIndex++;
        this.tablesIndex++;
      }
      //当数据遍历完 重新请求数据
      if (this.tablesIndex >= count) {
        this.tablesIndex = 0;
      }
    }

  }

  // 显示 天 时 分 换算
  toHourMinute(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if (Math.floor(minutes / 60 / 24) < 10) {
        return (Math.floor(minutes / 60 / 24) + "天");
      }
      return (Math.floor(minutes / 60 / 24) + "天");
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }

  }

  // 排序 倒序
  sortarr(key) {
    return function (a, b) {
      a[key] == undefined ? a[key] = 0 :
        b[key] == undefined ? b[key] = 0 : 0 ;
      return b[key] - a[key];
    };
  }
}
